package cn.yunhe.test;
import cn.yunhe.Dao.IEmpDao;
import cn.yunhe.Dao.impl.NamedEmpDao;
import cn.yunhe.entity.Dept;
import cn.yunhe.entity.Emp;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;

public class Demo1 {
    ApplicationContext ac = null;
    IEmpDao empDao = null;

  @Before
    public void init() {
        ac = new ClassPathXmlApplicationContext("spring.xml");
        empDao = (IEmpDao) ac.getBean("empDao");


    }

    @Test
    public void addTest() {
        Emp emp = new Emp();
        emp.setEmpno(3000);
        emp.setEname("wuwu");
        emp.setJob("xiaoshou");

        int result = empDao.saveEmp(emp);
        System.out.println("result====" + result);
    }

    @Test
    public void updateTest() {
        Emp emp = new Emp();
        emp.setEname("lisi");
        emp.setEmpno(8000);
        empDao.updateEmp(emp);
    }

    @Test
    public void delTest() {
        empDao.delEmp(7369);
    }

    @Test
    public void rowMapperTest() {
        List<Emp> list = empDao.getAllEmp();
        for (Emp e :
                list) {
            System.out.println(e.getEmpno() + "," + e.getEname() + "," + e.getDept().getDname());
        }
    }

    @Test
    public void rowMapperOneTest() {
        Emp emp = empDao.getEmpById(7369);
        System.out.println(emp.getEmpno() + "," + emp.getDept().getDname());
    }

    @Test
    public void dynamicQuery() {
        Emp emp = new Emp();
        emp.setEname("S");
        Dept dept = new Dept();
        dept.setDeptno(20);
        emp.setDept(dept);

        List<Emp> list = empDao.getEmpByPro(emp);

        for (Emp e :
                list) {
            System.out.println(e.getEmpno() + "," + e.getEname() + "," + e.getDept().getDname());
        }
    }

    @Test
    public void saveTest(){
        NamedEmpDao namedEmpDao= (NamedEmpDao) ac.getBean("namedEmpDao");
        Emp emp=new Emp();
        emp.setEmpno(9000);
        emp.setEname("66066");
        emp.setJob("9999");
        System.out.println(namedEmpDao.saveEmp(emp));
    }


}

